package com.tlc.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public abstract class BossServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ///接收用户信息，解决乱码问题
        request.setCharacterEncoding("utf-8");
        //设置响应内容，设置编码格式
        response.setContentType("text/html;charset=utf-8");
        //获取method的值
        String method = request.getParameter("method");
        //获取反射对象【BookServlet字节码对象】使用this可以扩大使用范围
        Class<? extends BossServlet> cla = this.getClass();
        //创建对象
        try {
            //有局限性
            //Object obj = cla.newInstance();
            //创建方法
            Method method1 = cla.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //暴力反射
            method1.setAccessible(true);
            //调用方法
            method1.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
